using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Site : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            SetState(States.View);
        }
    }

    protected void dsSite_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Site> e)
    {
        e.SetData(AcmsSite);
    }

    protected void dsSiteMappings_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.SiteMapping> e)
    {
        e.SetData(Acms.Bll.SiteMapping.GetAllBySiteId(AcmsSite.Id));
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        SetState(States.EditSite);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            if (dvSite.CurrentMode == DetailsViewMode.Edit)
            {
                dvSite.UpdateItem(true);
            }
            SetState(States.View);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        SetState(States.View);
    }

    protected void btnAddMapping_Click(object sender, EventArgs e)
    {
        SetState(States.AddMapping);
        dvSiteMappings.DataBind();
    }

    protected void btnSaveMapping_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            if (dvSiteMappings.CurrentMode == DetailsViewMode.Insert)
            {
                dvSiteMappings.InsertItem(true);
            }
            else if (dvSiteMappings.CurrentMode == DetailsViewMode.Edit)
            {
                dvSiteMappings.UpdateItem(true);
            }
            SetState(States.View);
        }
    }

    protected void btnCancelMapping_Click(object sender, EventArgs e)
    {
        SetState(States.View);
    }

    private enum States
    {
        View,
        EditSite,
        EditMapping,
        AddMapping
    }

    private void SetState(States state)
    {
        btnEdit.Visible = false;
        btnSave.Visible = false;
        btnCancel.Visible = false;
        btnAddMapping.Visible = false;
        btnSaveMapping.Visible = false;
        btnCancelMapping.Visible = false;

        phSiteMappingsDetails.Visible = false;
        phSiteMappingsGrid.Visible = false;

        switch (state)
        {
            case States.View:
                if (dvSite.CurrentMode != DetailsViewMode.ReadOnly)
                    dvSite.ChangeMode(DetailsViewMode.ReadOnly);
                phSiteMappingsGrid.Visible = true;
                btnEdit.Visible = true;
                btnAddMapping.Visible = true;
                break;
            case States.EditSite:
                if (dvSite.CurrentMode != DetailsViewMode.Edit)
                    dvSite.ChangeMode(DetailsViewMode.Edit);
                btnCancel.Visible = true;
                btnSave.Visible = true;
                break;
            case States.AddMapping:
                if (dvSite.CurrentMode != DetailsViewMode.ReadOnly)
                    dvSite.ChangeMode(DetailsViewMode.ReadOnly);
                if (dvSiteMappings.CurrentMode != DetailsViewMode.Insert)
                    dvSiteMappings.ChangeMode(DetailsViewMode.Insert);
                phSiteMappingsDetails.Visible = true;
                btnSaveMapping.Visible = true;
                btnCancelMapping.Visible = true;
                break;
            case States.EditMapping:
                dvSiteMappings.PageIndex = gvSiteMappings.SelectedIndex;
                if (dvSite.CurrentMode != DetailsViewMode.ReadOnly)
                    dvSite.ChangeMode(DetailsViewMode.ReadOnly);
                if (dvSiteMappings.CurrentMode != DetailsViewMode.Edit)
                    dvSiteMappings.ChangeMode(DetailsViewMode.Edit);
                phSiteMappingsDetails.Visible = true;
                btnSaveMapping.Visible = true;
                btnCancelMapping.Visible = true;
                break;

        }
    }

    protected void gvSiteMappings_SelectedIndexChanged(object sender, EventArgs e)
    {
        SetState(States.EditMapping);
    }

    protected void dsSiteMappings_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<Acms.Bll.SiteMapping> e)
    {
        if (e.Action == Acms.DataControls.UpdateAction.Insert)
        {
            e.Item.SiteId = AcmsSite.Id;
        }
    }
    protected void dsSite_UpdatedData(object sender, Acms.DataControls.UpdatingDataEventArgs<Acms.Bll.Site> e)
    {
        AcmsSite = e.Item;
    }
}
